package com.sanery.service.impl;

import org.apache.ibatis.session.SqlSession;

import com.sanery.dao.MyBatisUtil;
import com.sanery.dao.UserDao;
import com.sanery.entity.User;
import com.sanery.service.UserService;

public class UserServiceImpl implements UserService {

	SqlSession session = MyBatisUtil.getSession();
	private UserDao dao = session.getMapper(UserDao.class);

	@Override
	public Integer getUserCount(String regname) {
		Integer count = dao.havaCommendUserNameCount(regname);
		MyBatisUtil.closeSession(); // 关闭
		return count;
	}

	@Override
	public User getUser(String username, String password) {
		User user = dao.getUser(username, password);
		MyBatisUtil.closeSession(); // 关闭
		return user;
	}

	@Override
	public Integer addUser(String username, String password, String email) {
		Integer num = null;
		try {
			dao.addUser(username, password, email);
			session.commit();// 提交事务
			num = 1;

		} catch (Exception e) {
			num = 0;
			session.rollback(); // 有异常说明有问题，回滚
			throw new RuntimeException(e);
		}
		MyBatisUtil.closeSession(); // 关闭
		return num;
	}

}
